using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using Jonathan.SharpDB;
using System.Windows.Forms;

namespace Jonathan
{
    public partial class ctrlStart : UserControl
    {
        private frmMain m_FormMain;

        public ctrlStart(frmMain main)
        {
            InitializeComponent(main);

            this.m_FormMain = main;
            this.Dock = DockStyle.Fill;

            this.m_RandVerseCtrl.FormMain = main;
            this.m_RandVerseCtrl.Font = this.m_FormMain.ProgramData.DefaultFont.ToFont();
            this.m_DailyDevCtrl.Font = this.m_FormMain.ProgramData.DefaultFont.ToFont();

            // Load a random vese...
            this.GetRandomVerse();

            // Load a Daily Devotional...
            this.LoadDaily();
        }

        private void LoadDaily ()
        {
            string path = "";
            foreach (cJData.icDBInfo dbI in this.m_FormMain.ProgramData.DailyDatabases)
            {
                if (dbI.Name.Equals(this.m_FormMain.ProgramData.DefaultDaily))
                {
                    path = Common.DATA_FOLDER + dbI.Path;
                    break;
                }
            }

            if (!string.IsNullOrEmpty(path))
            {
                Database db = new Database(path);
                Query q = new Query(QueryType.SELECT, this.GetMonth(DateTime.Today.Month));
                q.SELECT("*");
                q.WHERE("Day", Operator.EQUALS, DateTime.Today.Day);
                if(DateTime.Now.Hour < 12)
                    q.WHERE("Time", Operator.EQUALS, "Morning");
                else
                    q.WHERE("Time", Operator.EQUALS, "Evening");

                db.RunQuery(q);

                this.m_DailyDevCtrl.SetData(q.Results.GetFieldString(2, 0), q.Results.GetFieldString(3, 0),
                    q.Results.GetFieldString(4, 0), q.Results.GetFieldString(5, 0));
            }
        }

        private string GetMonth (int month)
        {
            switch (month)
            {
                case 1:
                    return "January";
                case 2:
                    return "February";
                case 3:
                    return "March";
                case 4:
                    return "April";
                case 5:
                    return "May";
                case 6:
                    return "June";
                case 7:
                    return "July";
                case 8:
                    return "August";
                case 9:
                    return "September";
                case 10:
                    return "October";
                case 11:
                    return "November";
                case 12:
                default:
                    return "December";
            }
        }
        /// <summary>
        /// A Method to update the Font...
        /// </summary>
        public void UpdateFont ()
        {
            this.m_DailyDevCtrl.Font = this.m_FormMain.ProgramData.DefaultFont.ToFont();
            this.m_RandVerseCtrl.Font = this.m_FormMain.ProgramData.DefaultFont.ToFont();
        }

        #region - Random Verse -

        /// <summary>
        /// Load a random verse...
        /// </summary>
        private void GetRandomVerse()
        {
            List<cJData.icDBInfo> sources = new List<cJData.icDBInfo>();
            sources.AddRange(this.m_FormMain.ProgramData.BibleDatabases);
            sources.AddRange(this.m_FormMain.ProgramData.OtherDatabases);

            // Get Bible...
            Random rand = new Random(System.DateTime.Now.Millisecond);
            int randTemp= rand.Next(sources.Count);
            string bible = sources[randTemp].Name;
            string bibleFile = sources[randTemp].Path;

            Database dbBible = new Database(Common.DATA_FOLDER + bibleFile);

            // Get List of Books...
            Query q = new Query(QueryType.SELECT, Common.TABLE_BOOK_LIST);
            q.SELECT("BookName");

            dbBible.RunQuery(q);

            // Get Book...
            string book = q.Results.GetFieldString(0, rand.Next(q.RowsReturned));

            q = null;
            q = new Query(QueryType.SELECT, Common.TABLE_BOOK_LIST);
            q.SELECT("NumChapters");
            q.WHERE("BookName", Operator.EQUALS, book);

            dbBible.RunQuery(q);

            if (q.Results == null)
            {
                this.GetRandomVerse();
                return;
            }

            int chap = rand.Next(1, q.Results.GetFieldInt(0, 0));

            q = null;

            q = new Query(QueryType.SELECT, book);
            q.SELECT("Verse");
            q.SELECT("Text");
            q.WHERE("Chapter", Operator.EQUALS, chap);

            dbBible.RunQuery(q);

            if (q.Results == null || q.Results.RowCount == 0)
            {
                this.GetRandomVerse();
                return;
            }

            int verseTemp = rand.Next(0, q.RowsReturned - 1);
            string text = q.Results.GetFieldString(1, verseTemp);
            int verse = q.Results.GetFieldInt(0, verseTemp);

            string loc = book + " " + chap.ToString() + ":" + verse.ToString();
            this.m_RandVerseCtrl.SetVerse(text, loc, bible, book, chap.ToString(), verse.ToString());
        }

        #endregion
    }
}
